<?php

class RoleController extends GTL_Action {

    public function init() {
        parent::init();
        /* Initialize action controller here */
    }

    public function indexAction() {
        $this->_helper->redirector('form', 'role', '');
    }

    public function formAction() {
        $roleForm = new Form_RoleForm();
        $successmessage = "";
        $roleType = "";
        $listString = "";
        $roleData['lgn_id'] = "";
        $roleData['com_id'] = "";
        $errorMessageArray = Array();
        //Load Meta Model
        $Rolemodel = new Model_Role();


        //Load the roletype array for the select box in form 
        if ($this->USER->roletype == 'SuperContractor') {
            $contractorArray = $Rolemodel->getcontractorArray($this->USER->company_id);
            $roleForm->role_contractor->addMultiOptions($contractorArray);
        }if ($this->USER->roletype == 'SuperCustomer') {
            $customerArray = $Rolemodel->getcustomerArray($this->USER->usr_lang_id, $this->USER->company_id);
            $roleForm->role_customer->addMultiOptions($customerArray);
        } else {
            $typeArray = $Rolemodel->getRoletypeArray($this->USER->roletype);
            $roleForm->role_type->addMultiOptions($typeArray);
        }
        //get array for print the menu and action list 
        $resources_data = $Rolemodel->getResourcearray($this->USER->roletype, $this->USER->company_id);

        if ($this->getRequest()->isPost()) {
            $postedData = $this->_request->getPost();
            if ($this->USER->roletype == 'SuperContractor') {
                if ($postedData['role_contractor'] == "") {
                    $errorMessageArray[] = $this->ZFTRANSLATE->translate("role_required_contractor");
                }
                if ($postedData['role_data'] == "") {
                    $errorMessageArray[] = $this->ZFTRANSLATE->translate("role_required_resource");
                }
            } else if ($this->USER->roletype == 'SuperCustomer') {
                if ($postedData['role_customer'] == "") {
                    $errorMessageArray[] = $this->ZFTRANSLATE->translate("role_required_customer");
                }
                if (array_key_exists('role_data', $postedData)) {
                    if ($postedData['role_data'] == "") {
                        $errorMessageArray[] = $this->ZFTRANSLATE->translate("role_required_resource");
                    }
                }
            } else {
                if ($postedData['role_type'] == "") {
                    $errorMessageArray[] = $this->ZFTRANSLATE->translate("role_required_type");
                }
                if ($postedData['role_type'] == 1 && $postedData['admin_name'] == '') {
                    $errorMessageArray[] = $this->ZFTRANSLATE->translate("role_required_admin");
                }
                if (($postedData['role_type'] == 2 || $postedData['role_type'] == 3) && $postedData['company_name'] == '') {
                    $errorMessageArray[] = $this->ZFTRANSLATE->translate("role_required_company");
                }
                if ($postedData['role_data'] == "") {
                    $errorMessageArray[] = $this->ZFTRANSLATE->translate("role_required_resource");
                }
            }
            //check posted data is valid or not
            if (!$roleForm->isValid($postedData)) {
                $errorMessage = $roleForm->getMessages();
                foreach ($errorMessage as $_err) {
                    foreach ($_err as $_val) {
                        $errorMessageArray[] = $_val;
                    }
                }
            } else {
                if (count($errorMessageArray) > 0) {
                    
                } else {

                    if ($this->USER->roletype == 'SuperContractor') {
                        $Rolemodel->delete("lgn_id = '" . $postedData['role_contractor'] . "'");
                    } else if ($this->USER->roletype == 'SuperCustomer') {
                        $Rolemodel->delete("lgn_id = '" . $postedData['role_customer'] . "'");
                    } else {
                        //delete before save data
                        if ($postedData['role_type'] == 1) {
                            $Rolemodel->delete("lgn_id = '" . $postedData['admin_name'] . "'");
                        } else {
                            //if role type is customer or contractor(delete by company id)
                            $Rolemodel->delete("com_id = '" . $postedData['company_name'] . "'");
                        }
                    }
                    if (array_key_exists('role_data', $postedData)) {
                        foreach ($postedData['role_data'] as $key => $value) {
                            if ($this->USER->roletype == 'SuperContractor') {
                                $postedData['company_name'] = $this->USER->company_id;
                                $roleType = 2;
                            } else if ($this->USER->roletype == 'SuperCustomer') {
                                $postedData['company_name'] = $this->USER->company_id;
                                $roleType = 3;
                            } else {
                                $roleType = $postedData['role_type'];
                            }

                            if ($roleType == 1) {
                                $roleData['lgn_id'] = $postedData['admin_name'];
                                $where = "lgn_id='" . $roleData['lgn_id'] . "' and ";
                            } else if ($roleType == 2) {
                                $roleData['com_id'] = $postedData['company_name'];
                                $where = "com_id='" . $roleData['com_id'] . "' and ";
                            } else {
                                $roleData['com_id'] = $postedData['company_name'];
                                $where = "com_id='" . $roleData['com_id'] . "' and ";
                            }

                            $role_alldata = explode("_", $value);
                            $controller_name = $role_alldata[0];
                            $action_name = $role_alldata[1];
                            $roleData['updated_lgn_id'] = $this->USER->lgn_id;
                            $roleData['role_type'] = $roleType;
                            $roleData['menu_module_name'] = $controller_name;
                            $roleData['role_controller_name'] = strtolower($controller_name);
                            $roleData['role_action_name'] = strtolower($action_name);
                            $roleData['role_status'] = 1;
                            $roleData['role_createddate'] = date("Y-m-d H:i:s");
                            $roleData['role_updatedate'] = date("Y-m-d H:i:s");
                            $where.="role_controller_name='" . $roleData['role_controller_name'] . "'
                                and role_action_name='" . $roleData['role_action_name'] . "'";
                            //if ($rolecount == 0) {


                            if ($this->USER->roletype == 'SuperContractor') {
                                $roleData['com_id'] = $this->USER->company_id;
                                $roleData['role_type'] = 2;
                                $roleData['lgn_id'] = $postedData['role_contractor'];
                                $Rolemodel->save($roleData);
                            } else if ($this->USER->roletype == 'SuperCustomer') {
                                $roleData['com_id'] = $this->USER->company_id;
                                $roleData['role_type'] = 3;
                                $roleData['lgn_id'] = $postedData['role_customer'];
                                $Rolemodel->save($roleData);
                            } else {
                                //get contrctor loginid
                                if ($postedData['role_type'] == 2) {
                                    $contractorid = $Rolemodel->fetch_company_contractor($roleData['com_id']);
                                    foreach ($contractorid as $cont_key => $cont_value) {
                                        $roleData['lgn_id'] = $cont_value['lgn_id'];
                                        $Rolemodel->save($roleData);
                                    }
                                }
                                //get customer loginid
                                if ($postedData['role_type'] == 3) {
                                    $customerid = $Rolemodel->fetch_company_customer($roleData['com_id']);
                                    foreach ($customerid as $cust_key => $cust_value) {
                                        $roleData['lgn_id'] = $cust_value['lgn_id'];
                                        $Rolemodel->save($roleData);
                                    }
                                }
                                //get admin 
                                if ($postedData['role_type'] == 1) {
                                    $Rolemodel->save($roleData);
                                }
                            }
                        }
                    }
                    $successmessage = $this->ZFTRANSLATE->translate("role_update_successfully");
                    // }
                }
            }


            //Load the roles given by Admin
            if ($this->USER->roletype != 'SuperContractor' && $this->USER->roletype != 'SuperCustomer') {

                $roleType = $postedData['role_type'];
                if ($roleType == 1) {
                    $roleData['lgn_id'] = $postedData['admin_name'];
                    $where = "lgn_id='" . $roleData['lgn_id'] . "' ";
                    $admin_name = $Rolemodel->ajax_roletype($roleType, $roleData['lgn_id']);
                    $label_name = "<label>" . $this->ZFTRANSLATE->translate("role_frm_admin_name") . "
                             <span class='error-star'>*</span>:</label>";
                    $listString = $label_name . $admin_name;
                } else if ($roleType == 2 || $roleType == 3) {
                    $roleData['com_id'] = $postedData['company_name'];
                    $where = "com_id='" . $roleData['com_id'] . "' ";
                    $admin_name = $Rolemodel->ajax_roletype($roleType, $roleData['com_id']);
                    $label_name = "<label>" . $this->ZFTRANSLATE->translate("role_frm_company_name") . "
                                 <span class='error-star'>*</span>:</label>";
                    $listString = $label_name . $admin_name;
                }
            }
            //Load the roles given by supercontractor
            if ($this->USER->roletype == 'SuperContractor') {
                $roleType = 2;
                $where = "lgn_id='" . $postedData['role_contractor'] . "' ";
            }


            if ($this->USER->roletype == 'SuperCustomer') {
                $roleType = 3;
                $where = "lgn_id='" . $postedData['role_customer'] . "' ";
            }

            $roleAccess = $Rolemodel->fetchEntryById($roleType, $where);

            //$prefilledArray = array();
            foreach ($roleAccess as $key => $value) {

                for ($i = 0; $i < count($resources_data); $i++) {
                    if ($resources_data[$i]['controller_name'] == $value['role_controller_name']) {
                        for ($j = 0; $j < count($resources_data[$i]['action']); $j++) {
                            $listDisplayname = $this->ZFTRANSLATE->translate(($resources_data[$i]['action'][$j]['name']));
                            // echo $listDisplayname."==".$value['role_action_name']."<br>";
                            if ($resources_data[$i]['action'][$j]['chkbox_value'] == $value['role_action_name']) {
                                $resources_data[$i]['action'][$j]['checkbox_checked'] = 'true';
                            }
                        }
                    }
                }
            }
        }

        $this->view->listString = $listString;
        $this->view->log_id = $roleData['lgn_id'];
        $this->view->com_id = $roleData['com_id'];
        $this->view->user_roletype = $this->USER->roletype;
        $this->view->module_data = $resources_data;
        $this->view->errorMessageArray = $errorMessageArray;
        $this->view->succesMessage = $successmessage;
        $this->view->form = $roleForm;
    }

    //ajax resp for adminlisting and companylisting in dropdown
    public function admincompanylistAction() {
        $listString = "";
        if ($this->getRequest()->isPost()) {
            $postedData = $this->_request->getPost();
            $Rolemodel = new Model_Role();
            $listString = $Rolemodel->ajax_roletype($postedData['role_type']);
            if ($postedData['role_type'] == 1) {
                $label_name = "<label>" . $this->ZFTRANSLATE->translate("role_frm_admin_name") . "
                             <span class='error-star'>*</span>:</label>";
            } else {
                $label_name = "<label>" . $this->ZFTRANSLATE->translate("role_frm_company_name") . "
                             <span class='error-star'>*</span>:</label>";
            }
        }
        echo $label_name . $listString;
        exit;
    }

    //ajax resp for selected checkbox roll for admin.
    public function prefilledchecklistAction() {
        if ($this->getRequest()->isPost()) {
            $postedData = $this->_request->getPost();
            $Rolemodel = new Model_Role();
            // $adm_com_id = $postedData['adm_com_id'];
            $role_type = $postedData['role_type'];

            //Load the roles given by Admin
            $roleType = $postedData['role_type'];
            if ($roleType == 1) {
                $roleData['lgn_id'] = $postedData['adm_com_id'];
                $where = "lgn_id='" . $roleData['lgn_id'] . "' ";
                $admin_name = $Rolemodel->ajax_roletype($postedData['role_type'], $roleData['lgn_id']);
            } else if ($roleType == 2 || $roleType == 3) {
                $roleData['com_id'] = $postedData['adm_com_id'];
                $where = "com_id='" . $roleData['com_id'] . "' ";
                $admin_name = $Rolemodel->ajax_roletype($postedData['role_type'], $roleData['com_id']);
            }
            $roleAccess = $Rolemodel->fetchEntryById($role_type, $where);
            $prefilledArray = array();
            foreach ($roleAccess as $key => $value) {
                $control_actionName = $value['role_controller_name'] . "_" . ucfirst($value['role_action_name']);
                $prefilledArray[] = $control_actionName;
            }
        }
        echo json_encode($prefilledArray);
        exit;
    }

    //ajax resp for selected checkbox roll for contractor.
    public function contractorchecklistAction() {
        $listString = "";
        if ($this->getRequest()->isPost()) {
            $postedData = $this->_request->getPost();

            $Rolemodel = new Model_Role();
            $lgn_id = $postedData['lgn_id'];

            //Load the roles given by Admin
            $where = "lgn_id='" . $postedData['lgn_id'] . "'";
            $role_type = 2;
            $roleAccess = $Rolemodel->fetchEntryById($role_type, $where);
            $prefilledArray = array();
            foreach ($roleAccess as $key => $value) {
                $control_actionName = $value['role_controller_name'] . "_" . ucfirst($value['role_action_name']);
                $prefilledArray[] = $control_actionName;
            }
        }
        echo json_encode($prefilledArray);
        exit;
    }

    //ajax resp for selected checkbox roll for contractor.
    public function customerchecklistAction() {
        $listString = "";
        if ($this->getRequest()->isPost()) {
            $postedData = $this->_request->getPost();

            $Rolemodel = new Model_Role();
            $lgn_id = $postedData['lgn_id'];

            //Load the roles given by Admin
            $where = "lgn_id='" . $postedData['lgn_id'] . "'";
            $role_type = 3;
            $roleAccess = $Rolemodel->fetchEntryById($role_type, $where);
            $prefilledArray = array();
            foreach ($roleAccess as $key => $value) {
                $control_actionName = $value['role_controller_name'] . "_" . ucfirst($value['role_action_name']);
                $prefilledArray[] = $control_actionName;
            }
        }
        echo json_encode($prefilledArray);
        exit;
    }

}

?>
